package gavdi.dk.view;


import java.util.ArrayList;
import java.util.List;

import gavdi.dk.controller.Controller;
import gavdi.dk.data.DAOEvent;
import gavdi.dk.data.DAOUser;
import gavdi.dk.model.Event;
import gavdi.dk.model.User;
import gavdi.dk.view.R;
import gavdi.dk.util.Tools;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;


import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MenuActivity extends Activity implements OnClickListener {
	
	private User user;
	private Bundle bundle;
	private LinearLayout ll;
	private Button btnAllEvents;
	private Button btnLogoutFromMenu;
	private TextView lblWelcomeName;
	private String userID;
	
	private Button day1;
	private Button day2;
	private Button day3;
	private Button day4;
	private Button day5;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_layout);

        
        initAttributes();

       
  //      Controller.createLayoutEvents(getBaseContext(), Controller.getRecentEventsByUser(user), ll, 5);
        DAOEvent dEvent = new DAOEvent(getBaseContext());
        DAOUser dUser = new DAOUser(getBaseContext());
        

         Controller.createLayoutEvents(getBaseContext(),dEvent.getAmountOfEventsByUserID(userID, "7"), ll);
		
         

        if ( dUser.getUser(bundle.getString("userID")) != null )
        {
        	
        	lblWelcomeName.setText("Welcome " + dUser.getUser(bundle.getString("userID")).getName());
        }
        else
        	
        {
        	lblWelcomeName.setText("Welcome " + bundle.getString("userID"));
        }
        

        btnAllEvents.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {

				Intent intent = new Intent(getBaseContext(),EventActivity.class);

				intent.putExtras(bundle);

				

			    startActivity(intent);	

			}
		});
        
        btnLogoutFromMenu.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(getBaseContext(),LoginActivity.class);
				startActivity(intent);	
				
			}
		});
        
        day1.setOnClickListener(this);
        day2.setOnClickListener(this);
        day3.setOnClickListener(this);
        day4.setOnClickListener(this);
        day5.setOnClickListener(this);
  
    }
    
    public void onClick(View v) { 
    			
		String date = "";
		
		if ( v == day1)
		{
			date = Tools.getDate(4, false);
		}
		if ( v == day2)
		{
			date = Tools.getDate(3, false);
		}
		if ( v == day3)
		{
			date = Tools.getDate(2, false);
		}
		if ( v == day4)
		{
			date = Tools.getDate(1, false);
		}
		if ( v == day5)
		{
			date = Tools.getDate(0, false);
		}
		bundle.putString("date", date);
		
		Intent intent = new Intent(getBaseContext(), RegistrationActivity.class);
		intent.putExtras(bundle);
		startActivity(intent);	
    } 
		
	private void initAttributes(){
			bundle = getIntent().getExtras();
			userID = bundle.getString("userID");
		//	user = (User) bundle.getSerializable("user");
		
		
		ll = (LinearLayout)findViewById(R.id.listEventsInMenu);
		
		lblWelcomeName = (TextView) findViewById(R.id.lblWelcomeName);
		btnAllEvents = (Button) findViewById(R.id.btnAllEvents);
		btnLogoutFromMenu = (Button) findViewById(R.id.btnLogoutFromMenu);
		day1 = (Button) findViewById(R.id.btnDay1);
		day2 = (Button) findViewById(R.id.btnDay2);
		day3 = (Button) findViewById(R.id.btnDay3);
		day4 = (Button) findViewById(R.id.btnDay4);
		day5 = (Button) findViewById(R.id.btnDay5);
		
		
		day1.setText(Tools.getDate(4, true));
		day2.setText(Tools.getDate(3, true));
		day3.setText(Tools.getDate(2, true));
		day4.setText(Tools.getDate(1, true));
		day5.setText(Tools.getDate(0, true));


	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// --- save whatever you need to persist---

		outState.putString("userID", userID);
	//	outState.putBundle("bundle", bundle);

		super.onSaveInstanceState(outState);
		
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// ---retrieve the information persisted earlier---

		super.onRestoreInstanceState(savedInstanceState);

		userID = savedInstanceState.getString("userID");
	//	bundle = savedInstanceState.getBundle("bundle");
		
	}
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_layout, menu);
        return true;
    }
    
    
}
